home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gnome-python.idb / usr / freeware / lib / python1.5 / site-packages / gnome / affine.py.z / affine.py
Encoding:
Python Source  |  1999-07-16  |  2.3 KB  |  75 lines

  1. """A module that should help create the affine transformations you would use
  2. for transforming GnomeCanvas items.
  3.  
  4. As a little background, an affine transformation is a linear transformation
  5. and a translation.  The linear transformationcan be represented by a matrix
  6. and the translation by a vector.  So a general affine transformation looks
  7. like this:
  8.    [ x ] -> [ a b ][ x ] + [ e ]
  9.    [ y ]    [ c d ][ y ]   [ f ]
  10.  
  11. As you can see, the affine transformation is defined by the six numbers in
  12. the matrix and column vector.  For the GnomeCanvas item functions, affine
  13. transformations are represented by a flat sequence of the form
  14.   (a, c, b, d, e, f)
  15.   
  16. This module attempts to make it easy to create complex affine transformations
  17. from simpler ones like rotation, scaling and translation.
  18. """
  19.  
  20. import math
  21.  
  22. # These functions create base affine transformations that can be used to build
  23. # more complex ones.
  24.  
  25. def identity():
  26.     """The identity affine transformation"""
  27.     return (1, 0, 0, 1, 0, 0)
  28.  
  29. def scale(sx=1, sy=1):
  30.     """Scale by a factor of sx in the X direction and sy in Y direction"""
  31.     return (sx, 0, 0, sy, 0, 0)
  32.  
  33. def rotate(radians=None, degrees=0):
  34.     """Rotate in either degrees or radians"""
  35.     if not radians: radians = degrees * math.pi / 180.0
  36.     s = math.sin(radians)
  37.     c = math.cos(radians)
  38.     return (c, s, -s, c, 0, 0)
  39.  
  40. def shear(xshear=0, yshear=0):
  41.     """Creates a shear transformation in either X and/or Y directoion"""
  42.     return (1, yshear, xshear, 1, 0, 0)
  43.  
  44. def translate(tx=0, ty=0):
  45.     """Translate by (tx,ty)"""
  46.     return (1, 0, 0, 1, tx, ty)
  47.  
  48.  
  49. # These functions can be used to combine or manipulate other affine
  50. # tranformations
  51.  
  52. def compose(a1, *affines):
  53.     """Compose a number of affine transformations together
  54.  
  55.     If the affines a1,a2,a3 are passed as arguments, then the resulting
  56.     affine A will give A(x) == a1(a2(a3(x)))"""
  57.     if affines == (): return a1
  58.     a2 = apply(compose, affines)
  59.     return (a1[0]*a2[0] + a1[2]*a2[1],
  60.         a1[1]*a2[0] + a1[3]*a2[1],
  61.         a1[0]*a2[2] + a1[2]*a2[3],
  62.         a1[1]*a2[2] + a1[3]*a2[3],
  63.         a1[0]*a2[4] + a1[2]*a2[5] + a1[4],
  64.         a1[1]*a2[4] + a1[3]*a2[5] + a1[5])
  65.  
  66. def invert(aff):
  67.     det_inv = 1 / (aff[0]*aff[3] - aff[1]*aff[2])
  68.     return (aff[3]  * det_inv,
  69.         -aff[1] * det_inv,
  70.         -aff[2] * det_inv,
  71.         aff[0]  * det_inv,
  72.         (aff[2]*aff[5] - aff[3]*aff[4]) * det_inv,
  73.         (aff[1]*aff[4] - aff[0]*aff[5]) * det_inv)
  74.  
  75.